//
//  ZZUserBlockCell.m
//  QQplayground
//
//  Created by zhangxy on 2018/8/24.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "ZZUserBlockCell.h"

@implementation ZZUserBlockCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    _imgAvatar.layer.masksToBounds = YES;
    _imgAvatar.layer.cornerRadius =  22.0f;
    
    [_labName setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [_labName setFont:FontFiftent];
    
    [_labDesc setTextColor:UIColorFromRGB(Text6Color)];
    [_labDesc setFont:FontThirteen];
    _labDesc.hidden = YES;
    
    [_labTime setTextColor:UIColorFromRGB(Text6Color)];
    [_labTime setFont:FontThirteen];
    _labTime.hidden = YES;
    
    _btnAttent.layer.cornerRadius = 3.0f;
    _btnAttent.layer.masksToBounds  = YES;
    [_btnAttent.titleLabel setFont:FontFiftent];
    
    [_btnAttent addTarget:self action:@selector(attentClick:) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)attentClick:(UIButton *) btn{
    
        if([_tempUser.user_relation intValue]==0){
            // 解除拉黑
            [NetworkUtil POST:API_Delblack parameters:@{@"black_user_id":_tempUser.user_id,
                                                        @"session_uid":[UserUtil getUser].user_id}
                      success:^(id responseObject, NSDictionary *dict) {
                          _tempUser.user_relation = @"1";
                          [self setRelationView];
                      } failure:^(NSError *error, NSString *message) {
                          [PopUtil showMessage:convertToString(message)];
                      }];
        }else{
            // 拉黑
            [NetworkUtil POST:API_Addblack parameters:@{@"black_user_id":_tempUser.user_id,
                                                        @"session_uid":[UserUtil getUser].user_id}
                      success:^(id responseObject, NSDictionary *dict) {
                          _tempUser.user_relation = @"0";
                          [self setRelationView];
                      } failure:^(NSError *error, NSString *message) {
                          [PopUtil showMessage:convertToString(message)];
                      }];
        }
}


-(void)dataToView:(User *) model{
    _tempUser = model;
    [_imgAvatar sd_setImageWithURL:[NSURL URLWithString:convertToString(model.user_avatar)] placeholderImage:[UIImage imageNamed:@"default_image"]];
    [_labName setText:convertToString(model.user_name)];
    
    [self setRelationView];
    
    [self setFrame:CGRectMake(0, 0, ScreenWidth, 65)];
    
}

-(void)drawRect:(CGRect)rect{
    
    CGRect f = _btnAttent.frame;
    if([_tempUser.user_relation intValue]==0){
        f.size.width = 89.0f;
        f.origin.x = ScreenWidth - 89 - 20;
        _btnAttent.frame = f;
    }else{
        f.size.width = 66.0f;
        f.origin.x = ScreenWidth - 66 - 20;
        _btnAttent.frame = f;
    }
    
    if(convertToString(_labDesc.text).length > 0){
        _labDesc.hidden = NO;
    }
}

-(void)setRelationView{
    CGRect f = _btnAttent.frame;
    
    if([_tempUser.user_relation intValue]==0){
        [_btnAttent setTitleColor:UIColorFromRGB(Text6Color) forState:0];
        _btnAttent.layer.borderColor = UIColorFromRGB(Text6Color).CGColor;
        _btnAttent.layer.borderWidth = 1.0f;
        [_btnAttent setBackgroundColor:UIColorFromRGB(TextWhiteColor)];
        [_btnAttent setTitle:@"解除拉黑" forState:0];
        
        f.size.width = 89.0f;
        f.origin.x = ScreenWidth - 89 - 20;
        _btnAttent.frame = f;
    }else{
        [_btnAttent setTitleColor:UIColorFromRGB(TextWhiteColor) forState:0];
        _btnAttent.layer.borderColor = UIColorFromRGB(Text6Color).CGColor;
        _btnAttent.layer.borderWidth = .0f;
        [_btnAttent setBackgroundColor:UIColorFromRGB(BgTitleColor)];
        [_btnAttent setTitle:@"拉黑" forState:0];
        
        f.size.width = 66.0f;
        f.origin.x = ScreenWidth - 66 - 20;
        _btnAttent.frame = f;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
